<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
</head>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
    function ocr() {
        var filename = document.getElementById("file").value;
        var form = document.forms["ff"];
        if (filename != "") {
            try {
                var obj = new ActiveXObject("ADODB.Stream");//这个必然是IE
            } catch (e) {
                var file = form["file"].files[0];
                var reader = new FileReader();
                reader.readAsBinaryString(file);//这个读法是异步的
                reader.onloadend = function () {
                    // 这个事件在读取结束后，无论成功或者失败都会触发
                    if (reader.error) {
                        console.log(reader.error);
                    } else {
                        uploadAndSubmit2(reader.result);
                    }
                }
                return;
            }
            // var bf1 = new BinaryFile(filename);//这个读法是同步的
            // uploadAndSubmit2(bf1.ReadAll());
        }
    }

    //给XMLHttpRequest的原型添加二进制发送功能
    XMLHttpRequest.prototype.sendAsBinary = function (datastr) {
        function byteValue(x) {
            return x.charCodeAt(0) & 0xff;
        }
        var ords = Array.prototype.map.call(datastr, byteValue);
        var ui8a = new Uint8Array(ords);
        this.send(ui8a.buffer);
    }

    function uploadAndSubmit2(BinaryContent) {
        var Url = "http://101.132.175.95:8002/table";
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open("POST", Url);
        xmlHttp.sendAsBinary(BinaryContent);
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var str = xmlHttp.response;
                    alert(str);
                    xmlHttp.abort();
                }
            }
        }
    }
</script>
<style>

</style>
<body>
<img height="120px"
     src="https://s3mm1.abcpen.com/154339106190822236925.png"></img>
<br/>
<input type="radio" name="mode" value="hand-writing" checked="checked"/> 手写文字 <input type="radio" name="mode"
                                                                                     value="general"/> 普通文字
<br/>
<div style="padding-top:10px;">
    <form id="ff">
        <input id="file" type="file" value="" name="file"/>
        <button id="btn" type="button" onclick="ocr()">上传</button>
        <span id="ss"></span>
    </form>

</div>
<br/>
<div>
    <div id="dd"></div>
</div>
</body>
</html>